﻿using Simple.Common;
using Simple.Entity.Dto;
using Simple.Entity.Entity;
using Simple.Entity.Params;
using Simple.Entity.View;
using Simple.IRepository;
using Simple.IService;
using Simple.Tools;
using Simple.Tools.Verify;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.Service
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public class RoleService:BaseService,IRoleService
	{
		[Autowired]

		protected ISimpleMapper mapper { get; set; }	
		[Autowired]

		protected IRoleRepository roleRepository { get; set; }

		[Autowired]

		protected IRoleMenuRepsitory  roleMenuRepsitory { get; set; }

		[Autowired]
		protected IUserRoleRepository userRoleRepository { get; set; }

		[Autowired]
		protected IUserMenuRepository userMenuRepository { get; set; }	

		[Autowired]

		protected IMenuRepository menuRepository { get; set; }

		public RoleService() { }

		/// <summary>
		/// 创建角色
		/// </summary>
		/// <param name="roleDto"></param>
		/// <returns></returns>
		public async  Task<Common.BaseMessage> AddAsync(RoleDto roleDto)
		{
			SysRoleEntity role = new SysRoleEntity(true);
			if (await roleRepository.AnyAsync(r=>r.RoleName==role.RoleName && r.IsDelete==false && r.RoleType==role.RoleType))
			{
				return Bad400("添加失败,角色名称重复!");
			}

			if (await roleRepository.AnyAsync(r => r.RoleCode == role.RoleCode && r.IsDelete == false && r.RoleType == role.RoleType))
			{
				return Bad400("添加失败,角色code重复!");
			}
			mapper.Map(roleDto,role);

			var result=await roleRepository.UpdateAsync(role);

			if (result)
			{
				return Ok200("添加成功!");
			}
			return Bad400("添加失败!");
		}
		/// <summary>
		/// 更新角色
		/// </summary>
		/// <param name="roleDto"></param>
		/// <returns></returns>
		public async Task<Common.BaseMessage> UpdateAsync(long Id,RoleDto roleDto)
		{


			if (!await roleRepository.AnyAsync(r => r.Id==Id && r.IsDelete==false))
			{
				return Bad400("更新失败!");
			}

			SysRoleEntity role = await roleRepository.FirstOrDefaultAsync(r => r.Id == Id && r.IsDelete == false);

			if (await roleRepository.AnyAsync(r => r.Id!=Id&& r.RoleName == roleDto.RoleName && r.IsDelete == false && r.RoleType == roleDto.RoleType))
			{
				return Bad400("更新失败,角色code重复!");
			}

			if (await roleRepository.AnyAsync(r => r.Id!=Id&& r.RoleCode == roleDto.RoleCode && r.IsDelete == false && r.RoleType == roleDto.RoleType))
			{
				return Bad400("更新失败,角色code重复!");
			}
			mapper.Map(roleDto, role);

			var result = await roleRepository.UpdateAsync(role);

			if (result)
			{
				return Ok200("更新成功!");
			}
			return Bad400("更新失败!");
		}
		/// <summary>
		/// 删除角色
		/// </summary>
		/// <param name="Id"></param>
		/// <returns></returns>
		public async Task<Common.BaseMessage> SoftDeleteAsync(long Id)
		{
			if (!await roleRepository.AnyAsync(r => r.Id == Id && r.IsDelete == false))
			{
				return Bad400("删除失败!");
			}
			if (await userRoleRepository.AnyAsync(r=>r.RoleId==Id))
			{
				return Bad400("已有用户绑定该角色,清先解绑角色!");
			}
			var role=await roleRepository.FirstOrDefaultAsync(r => r.Id == Id && r.IsDelete == false);
			role.IsDelete = true;
			var result=await roleRepository.UpdateAsync(role);
			if (result)
			{
				return Ok200("角色移除成功!");
			}
			return Bad400("角色移除失败!");
		}
		/// <summary>
		/// 删除角色菜单
		/// </summary>
		/// <param name="Id">角色菜单关联id</param>
		/// <returns></returns>
		public async Task<Common.BaseMessage> DeleteMenuAsync(long Id)
		{
			if (!await roleMenuRepsitory.AnyAsync(r => r.Id == Id))
			{
				return Bad400("删除失败!");
			}
			#region 移除所有用户的该菜单
			var result=await roleMenuRepsitory.DeleteAsync(r=>r.Id==Id);
			#endregion

			if (result)
			{
				return Ok200("角色移除菜单成功!");
			}
			return Bad400("角色移除菜单失败!");
		}

		/// <summary>
		/// 绑定角色菜单
		/// </summary>
		/// <param name="Id"></param>
		/// <returns></returns>
		public async Task<Common.BaseMessage> BindMenuAsync(RoleDto roleDto)
		{
			long Id = roleDto.Id.ToInt64();
			if (!await roleRepository.AnyAsync(r => r.Id == Id && r.IsDelete == false))
			{
				return Bad400("角色不存在!");
			}
			await roleRepository.FirstOrDefaultAsync(r=>r.Id== Id);

			long[] ids = roleDto.MenuIds.Select(m=>m.ToInt64()).ToArray();

			 ids = await menuRepository.Where(m=> ids.Contains(m.Id)).Select(m=>m.Id).ToArrayAsync();

			if (SimpleCheck.IsNullOrEmpty( ids))
			{
				return Bad400("请选择正确的菜单!");
			}
			List<SysRoleMenuEntity>sysRoleMenus = new List<SysRoleMenuEntity>();
			foreach (var item in ids)
			{
				sysRoleMenus.Add(new SysRoleMenuEntity(true)
				{
					MenuId=item,
					RoleId=Id
				});
			}

			var result=await roleMenuRepsitory.UpdateAsync(sysRoleMenus);
			if (result)
			{
				return Ok200("角色移除成功!");
			}
			return Bad400("角色移除失败!");
		}

		/// <summary>
		/// 分页查询
		/// </summary>
		/// <param name="Params"></param>
		/// <returns></returns>
		public async Task<Common.BaseMessage> GetPageAsync(RoleParameter Params)
		{
			var query = roleRepository.Query(q =>
			{

			});
			return Ok200(await PageList<RoleView>.CreateAsync(query,Params));
		}

	}
}
